﻿using System;
using System.Linq;
using NUnit.Framework;
using Nydus.Model.Entities;

namespace Nydus.Model.Repositories.Tests
{
    [TestFixture]
    class TestCustomerRepository
    {
        private UnitOfWork uow = null;
        private CustomerRepository customerRepository=null;

        [SetUp]
        public void SetUp()
        {
            uow = new UnitOfWork();
            customerRepository = new CustomerRepository(uow);
        }

        [Test]
        public void Deactivate_ExistingCustomer_Deactivated()
        {
            // Add customer to the repository
            Customer cust = new Customer();
            string customerName = "Rowery SA";
            cust.Name = customerName;
            cust.DateFrom = DateTime.Now;
            customerRepository.Add(cust);

            Assert.IsTrue(cust.Id == 0);
            uow.Save();
            Assert.IsTrue(cust.Id > 0);
            
            // Deactivate the customer
            int customerId = cust.Id;
            Customer cust1 = customerRepository.GetById(customerId);
            customerRepository.Delete(cust1);
            uow.Save();

            // Verify the customer can be queried by id
            Customer cust2 = customerRepository.GetById(customerId);
            Assert.IsTrue(cust2 != null);

            // Verify the customer is on the list of all customers
            Customer cust3 = customerRepository.All.Where(c => c.Id == customerId).FirstOrDefault();
            Assert.IsTrue(cust3 != null);

            // Verify the customer is not on the list of active customers
            Customer cust4 = customerRepository.Active.Where(c => c.Id == customerId).FirstOrDefault();
            Assert.IsTrue(cust4 == null);
        }
    }
}
